from django import forms
from django.core.validators import RegexValidator

from app01 import models


class RegisterForm(forms.ModelForm):
    mobile_phone = forms.CharField(label='手机号', validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误'),],
                                   widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'请输入手机号'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control', 'placeholder':'请输入密码'}), label='密码')
    confirm_password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control', 'placeholder':'请重复输入密码'}), label='确认密码')
    code = forms.CharField(label='验证码', widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'请输入验证码'}))

    class Meta:
        model = models.UserInfo
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入%s'%(field.label,)
